var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var root = ui("$");
var http = require("helper/http");
var openHelper = require("helper/open");
var imHelper = require("helper/im");
var circleUrl = require("url/circle");
var friendHelper = require("helper/friend");

var httpUrl4Detail = circleUrl.url.member.detail4manage;

var isUpdate = false;
var id = null;
var memberId = null;
var circleId = null;
var type = null;
var isFriend = false;//是否好友
page.on("back", function() {
	app.closePage({member_update:isUpdate});
});

page.on("loaded", function() {
	id = page.getData().id;
	getMemberInfo();
});
var headerAout = ui("headerAout");
//加载头部
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var headerLeft = ui(addheader + ".headerLeft");
headerLeft.on("touch", function() {
	app.closePage({member_update:isUpdate});
});
ui(addheader + ".labelTitle").text = "";//呢称


var layoutMain = ui("layoutMain");
/*移出圈子*/
var addMove = layoutMain.add("move", "source://view/reuse/form/mutiLabel.ui",0,0);
var move = ui("move");
move.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "移出圈子",
    "labelText":"",
    "imgForwardVisible": false
});
move.refreshData();
move.on("touch", function(data, e) {
	if(type=='1'){//群主
		nf.toast("不能踢走群主");
	}else if(type=='2'){
		nf.toast("不能踢走管理员");
	}else{
		nf.confirm("你确定要踢走该成员吗?", "", "确定", "取消", function(data, e) {
			if (data == 1) {
				http.post(circleUrl.url.member.kickMember, {memberId:memberId,circleId:circleId}, function(data) {
					nf.toast("踢人成功");
					app.closePage({kick_member:true});
				});
			}
		})
		
	}
});

/*设置管理员*/
var addSwitch4admin = layoutMain.add("switch4admin","source://view/reuse/form/mutiSwitch.ui");
var switch4admin = ui("switch4admin");
switch4admin.bindData(hashdata);
hashdata.addData({"labelTitle" : "设为管理员"});
switch4admin.refreshData();
switch4admin.on("switchtag", function(data) {
	if (data == 0) {// 关闭，删除管理员身份
		http.post(circleUrl.url.member.cancleCircleManager, {memberId:memberId,circleId:circleId}, function(data) {
			//nf.toast("取消管理员成功");
			type = 3;//成员类型:1-群主;2-管理员;3-普通成员
		});
	}
	if (data == 1) {// 打开，任命管理员身份
		http.post(circleUrl.url.member.setCircleManager, {memberId:memberId,circleId:circleId}, function(data) {
			//nf.toast("设置管理员成功");
			type = 2;//成员类型:1-群主;2-管理员;3-普通成员
		});
	}
});

/*加入时间*/
var addJoinTime = layoutMain.add("joinTime", "source://view/reuse/form/mutiLabel.ui",0,0);
var joinTime = ui("joinTime");
joinTime.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "加入时间",
    "labelText":"2018-06-06",
    "imgForwardVisible": false
});
joinTime.refreshData();


/*查看用户信息*/
var addViewInfo = layoutMain.add("viewInfo", "source://view/reuse/form/mutiLabel.ui",0,0);
var viewInfo = ui("viewInfo");
viewInfo.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "查看用户信息",
    "labelText":"",
    "imgForwardVisible": false
});
viewInfo.refreshData();
viewInfo.on("touch", function(data, e) {
	if(memberId !=null){
		openHelper.openPersonal(memberId);
	}
});
/*************function***************/
function getMemberInfo(){
	http.post(httpUrl4Detail,{id:id},function(data){
		var member = data.member;
		isFriend = data.isFriend;
		if(isFriend){
			ui("imgFriend").source="source://image/menu/userdel.png";
			ui("lableFriend").text="删除好友";
		}else{
			ui("imgFriend").source="source://image/menu/useradd.png";
			ui("lableFriend").text="加为好友";
		}
		if(member){
			//deviceone.print(JSON.stringify(member))
			circleId = member.circleId;
			memberId = member.memberId;
			type = member.type;//成员类型:1-群主;2-管理员;3-普通成员
			if(type =="2"){
				switch4admin.fire("switchzt", [ 1, " ", " " ]);//管理员
			}else{
				switch4admin.fire("switchzt", [ 0, " ", " " ]);//非管理员
			}
						
			ui(addJoinTime +".labelText").text = member.strNewAt.substring(0,10);
			ui(addheader + ".labelTitle").text = member.memberName;
			ui("imageViewLogo").source = member.fullMemberImg;
			if(member.sex==2){//女
				ui("labelSex").text="♀";
				ui("layoutSex").bgColor = "F883B8FF";
			}else{//男
				ui("labelSex").text="♂";
				ui("layoutSex").bgColor = "3EC8B1FF";
			}
		}
	});
}

ui("aLayoutUserIm").on("touch", function(data, e) {
	if(memberId != null){
		imHelper.toChat(memberId);
	}
})
//添加好友
ui("aLayoutFriend").on("touch", function(data, e) {
	if(isFriend){
		//如果已为好友，再次点击则删除好友
		nf.confirm("您确定要删除这个好友吗?","删除好友", "确定", "取消", function(data, e) {
			if (data == 1) {
				friendHelper.deleteFriend(memberId,function(data){
					isFriend = false;
					ui("imgFriend").source="source://image/menu/useradd.png";
					ui("lableFriend").text="加为好友";
					nf.toast("删除好友成功");
					imHelper.removeHistoryMessage(memberId);
				});
			}
		})
	}else{
		friendHelper.addFriendApply(memberId);
	}
})
